<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--用户数据-->
      <el-col :span="24" :xs="24">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
                 label-width="68px">
          <el-form-item label="用户名称" prop="name">
            <el-input
              v-model="queryParams.name"
              placeholder="请输入用户名称"
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="手机号码" prop="userPhone">
            <el-input
              v-model="queryParams.userPhone"
              placeholder="请输入手机号码"
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="创建时间">
            <el-date-picker
              v-model="dateRange"
              style="width: 240px"
              value-format="yyyy-MM-dd"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            ></el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-table v-loading="loading" :data="userList">
          <el-table-column label="名称" align="center" key="userName" prop="userName" v-if="columns[0].visible"/>
          <el-table-column label="电话号码" align="center" key="userPhone" prop="userPhone" v-if="columns[1].visible"/>
          <el-table-column label="认证状态" align="center" key="status" v-if="columns[2].visible">
            <template slot-scope="scope">
              <span v-for="item in statusList" v-if="item.key == scope.row.status">{{ item.value }}</span>
            </template>
          </el-table-column>
          <el-table-column label="运营商状态" align="center" key="operatorStatus" v-if="columns[3].visible">
            <template slot-scope="scope">
              <span v-if="scope.row.operatorStatus == 0">未认证</span>
              <span v-if="scope.row.operatorStatus == 1">认证中</span>
              <span v-if="scope.row.operatorStatus == 2">认证成功</span>
              <span v-if="scope.row.operatorStatus == 3">认证失败</span>
            </template>
          </el-table-column>
          <el-table-column label="创建时间" align="center" prop="createTime" v-if="columns[2].visible" width="160">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            width="160"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
              >
                <router-link :to="'/app/user-info/index/0/' + scope.row.userId" class="link-type">
                  <span>查看</span>
                </router-link>
              </el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
              >删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>
  </div>
</template>

<script>
import {listUser, deleteUserById} from "@/api/app/user/index";

export default {
  name: "AppUser",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      userList: null,
      // 日期范围
      dateRange: [],
      // 表单参数
      form: {},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        userPhone: undefined
      },
      // 列信息
      columns: [
        {key: 0, label: `名称`, visible: true},
        {key: 1, label: `电话号码`, visible: true},
        {key: 2, label: `认证状态`, visible: true},
        {key: 3, label: `运营商状态`, visible: true}
      ],
      statusList: [
        {
          key: 0,
          value: '初始化'
        },
        {
          key: 1,
          value: '基础信息1'
        },
        {
          key: 2,
          value: '基础信息2'
        },
        {
          key: 3,
          value: '身份证上传'
        },
      ],
    };
  },

  created() {
    this.getList();
  },
  methods: {
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      listUser(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.userList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },

    // toInfo(row) {
    //   this.$router.push({
    //     path: '/app/user-info/index/0/' + row.userId,
    //   })
    // },

    handleDelete(row) {
      this.$modal.confirm('是否删除？').then(function () {
        return deleteUserById(row.userId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("成功");
      }).catch((e) => {
      });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
  }
};
</script>
